package com.shelpe.services.sinbad.controller.actionLog.impl.campaign;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.controller.actionLog.ActionLogDto;
import com.shelpe.services.sinbad.controller.actionLog.ActionLogService;
import com.shelpe.services.sinbad.controller.actionLog.CampaignVo;
import com.shelpe.services.sinbad.entity.jpa.AreaList;
import com.shelpe.services.sinbad.entity.jpa.Campaign;
import com.shelpe.services.sinbad.entity.jpa.CampaignLog;

@Service
public class CampaignUpdatePlatformConverter extends ActionLogService {
	@Autowired
	private MessageSource messageSource;

	@Override
	public ActionLogDto convertCampaign(CampaignLog entity, Map<Long, Campaign> campaignMap, List<AreaList> areas) {
		boolean campaignUpdatePlatformConverter = entity.getAction() == CampaignLog.Action.UPDATE_PLATFORM
				|| entity.getAction() == CampaignLog.Action.AUTO_UPDATE_PLATFORM;
		if (campaignUpdatePlatformConverter && StringUtils.isNotBlank(entity.getNote())) {
			Campaign campaign = campaignMap.get(entity.getCampaignID());
			CampaignVo vo = this.unserializeCampaign(entity.getNote());
			ActionLogDto dto = new ActionLogDto();
			dto.from(entity);
			if (campaign != null && vo != null && vo.getAddPlatform() != null && vo.getLessPlatform() != null) {
				String[] params = { campaign.getTitle(), this.from(vo) };
				dto.setReason(messageSource.getMessage("message.campaign.platform.reason", params, Locale.ENGLISH));
				return dto;
			}
		}
		return null;
	}

	private String from(CampaignVo vo) {
		StringBuffer buff = new StringBuffer();
		StringBuffer buffAddSerch = new StringBuffer();
		buffAddSerch.append("增加的搜索推广:");
		StringBuffer buffLessSerch = new StringBuffer();
		buffLessSerch.append("减少的搜索推广:");
		StringBuffer buffAddNonSerch = new StringBuffer();
		buffAddNonSerch.append("增加的定向推广:");
		StringBuffer buffLessNonSerch = new StringBuffer();
		buffLessNonSerch.append("减少的定向推广:");
		if (!vo.getAddPlatform().isPcNonSearchIn() && vo.getLessPlatform().isPcNonSearchIn()) {
			buffAddNonSerch.append("淘宝站内");
		} else if (vo.getAddPlatform().isPcNonSearchIn() && !vo.getLessPlatform().isPcNonSearchIn()) {
			buffLessNonSerch.append("淘宝站内");
		}
		if (!vo.getAddPlatform().isPcNonSearchOut() && vo.getLessPlatform().isPcNonSearchOut()) {
			buffAddNonSerch.append(" 淘宝站外");
		} else if (vo.getAddPlatform().isPcNonSearchOut() && !vo.getLessPlatform().isPcNonSearchOut()) {
			buffLessNonSerch.append(" 淘宝站外");
		}
		if (!vo.getAddPlatform().isWireIn() && vo.getLessPlatform().isWireIn()) {
			buffAddNonSerch.append(" 无线站内");
		} else if (vo.getAddPlatform().isWireIn() && !vo.getLessPlatform().isWireIn()) {
			buffLessNonSerch.append(" 无线站内");
		}
		if (!vo.getAddPlatform().isWireOut() && vo.getLessPlatform().isWireOut()) {
			buffAddNonSerch.append(" 无线站外");
		} else if (vo.getAddPlatform().isWireOut() && !vo.getLessPlatform().isWireOut()) {
			buffLessNonSerch.append(" 无线站外");
		}
		if (StringUtils.isBlank(buffAddNonSerch.substring(buffAddNonSerch.indexOf(":") + 1))) {
			buffAddNonSerch.append("无");
		}
		if (StringUtils.isBlank(buffLessNonSerch.substring(buffLessNonSerch.indexOf(":") + 1))) {
			buffLessNonSerch.append("无");
		}

		if (!vo.getAddPlatform().isPcSearchIn() && vo.getLessPlatform().isPcSearchIn()) {
			buffAddSerch.append("淘宝站内");
		} else if (vo.getAddPlatform().isPcSearchIn() && !vo.getLessPlatform().isPcSearchIn()) {
			buffLessSerch.append("淘宝站内");
		}
		if (!vo.getAddPlatform().isPcSearchOut() && vo.getLessPlatform().isPcSearchOut()) {
			buffAddSerch.append(" 淘宝站外");
		} else if (vo.getAddPlatform().isPcSearchOut() && !vo.getLessPlatform().isPcSearchOut()) {
			buffLessSerch.append(" 淘宝站外");
		}
		if (!vo.getAddPlatform().isWireIn() && vo.getLessPlatform().isWireIn()) {
			buffAddSerch.append(" 无线站内");
		} else if (vo.getAddPlatform().isWireIn() && !vo.getLessPlatform().isWireIn()) {
			buffLessSerch.append(" 无线站内");
		}
		if (!vo.getAddPlatform().isWireOut() && vo.getLessPlatform().isWireOut()) {
			buffAddSerch.append(" 无线站外");
		} else if (vo.getAddPlatform().isWireOut() && !vo.getLessPlatform().isWireOut()) {
			buffLessSerch.append(" 无线站外");
		}
		if (StringUtils.isBlank(buffAddSerch.substring(buffAddSerch.indexOf(":") + 1))) {
			buffAddSerch.append("无");
		}
		if (StringUtils.isBlank(buffLessSerch.substring(buffLessSerch.indexOf(":") + 1))) {
			buffLessSerch.append("无");
		}

		StringBuffer buffOutsideDiscount = new StringBuffer();
		if (vo.getAddPlatform().getOutsideDiscount() != vo.getLessPlatform().getOutsideDiscount()) {
			buffOutsideDiscount.append("pc折扣率由" + vo.getAddPlatform().getOutsideDiscount() + "%修改为"
					+ vo.getLessPlatform().getOutsideDiscount() + "%");
		}
		StringBuffer buffMobileDiscounts = new StringBuffer();
		if (vo.getAddPlatform().getMobileDiscount() != vo.getLessPlatform().getMobileDiscount()) {
			buffMobileDiscounts.append("无线折扣率由" + vo.getAddPlatform().getMobileDiscount() + "%修改为"
					+ vo.getLessPlatform().getMobileDiscount() + "%");
		}

		buff.append(buffAddSerch);
		buff.append(";");
		buff.append(buffLessSerch);
		buff.append(";");
		buff.append(buffAddNonSerch);
		buff.append(";");
		buff.append(buffLessNonSerch);
		buff.append(buffOutsideDiscount != null && buffOutsideDiscount.length() > 0
				? ";" + buffOutsideDiscount.toString() : "");
		buff.append(buffMobileDiscounts != null && buffMobileDiscounts.length() > 0
				? ";" + buffMobileDiscounts.toString() : "");
		return buff.toString();
	}
}
